// 引入crypto加密模块 - Nodejs内置模块
let crypto;
try {
  crypto = require('node:crypto');
} catch (err) {
  console.log('crypto support is disabled!');
}

// MD5封装
function md5HashCrypto(password){
    const hash = crypto.createHash('md5');
    hash.update(password);
    const md5Password = hash.digest('hex');  // hash.digest() 返回2进制加密数据  hash.digest('hex')返回16进制加密数据
    return md5Password;
}

const secret = 'hellom';   // 加盐(加密的秘钥)
function md5HmacCrypto(password){
    const hmac = crypto.createHmac('sha256',secret);
    hmac.update(password);
    const md5Password = hmac.digest('hex');  // hmac.digest() 返回2进制加密数据  hmac.digest('hex')返回16进制加密数据
    return md5Password;
}

module.exports = { md5HashCrypto, md5HmacCrypto };